//
//  AXLoginViewController.m
//  AnXin100
//
//  Created by 天质弘耕 on 2018/10/10.
//  Copyright © 2018年 tzhg. All rights reserved.
//

#import "AXLoginViewController.h"
#import "UIView+SDAutoLayout.h"
#import "ZXCategories.h"
#import "AXRegisterViewController.h"
#import "AXFindPasswordViewController.h"
#import "HBDNavigationController.h"
#import "UIViewController+HBD.h"
#import "AppDelegate.h"
#import "YJSegmentedControl.h"


#define ZX_WEAK_SELF __weak typeof(self) weakSelf = self;

@interface AXLoginViewController ()<YJSegmentedControlDelegate>

/** logo */
@property (nonatomic ,strong) UIImageView *logoImageView;
/** 安心即刻启程label */
@property (nonatomic ,strong) UILabel *axLabel;
//分段控制属性
@property (nonatomic, strong)  YJSegmentedControl *segmentedView;
@property (nonatomic, assign) NSInteger requstIndex;
/**  手机图标*/
@property (nonatomic, strong) UIImageView *sjImageview;
/** 手机号码 */
@property (nonatomic ,strong) UITextField *sjTextField;
/** 验证码图标 */
@property (nonatomic ,strong) UIImageView *yzmImageview;
/** 验证码 */
@property (nonatomic ,strong) UITextField *yzmTextField;
/**  获取验证码按钮*/
@property (nonatomic ,strong) UIButton *hqyzmButton;
/** 登录按钮 */
@property (nonatomic ,strong) UIButton *loginButton;
/** 注册按钮 */
@property (nonatomic ,strong) UIButton *registerButton;
/** 忘记密码按钮 */
@property (nonatomic ,strong) UIButton *forgetButton;
/**  qq按钮*/
@property (nonatomic ,strong) UIButton *qqButton;
/**  微信按钮*/
@property (nonatomic ,strong) UIButton *wxButton;
/** 微博按钮 */
@property (nonatomic ,strong) UIButton *wbButton;
/** lgView */
@property (nonatomic , strong) UIView *lgView;

/** sessionId */
@property (nonatomic , strong) NSString *sessionId;
/** 倒计时 */
@property (nonatomic, strong)   NSTimer *timer;


@end

@implementation AXLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    // Do any additional setup after loading the view.
    [self creatView];
    [self setlayout];
    
 
    
    
    
}

- (void)creatView {
    
    /** logo */
    self.logoImageView = [[UIImageView alloc]init];
    self.logoImageView.image = [UIImage imageNamed:@"logo"];
    [self.view addSubview:self.logoImageView];
    /** 安心即刻启程 */
    self.axLabel = [[UILabel alloc]init];
    self.axLabel.text = @"安心即刻启程 !";
    self.axLabel.textAlignment = NSTextAlignmentCenter;
    self.axLabel.font = [UIFont systemFontOfSize:20];
    self.axLabel.textColor = [UIColor colorWithHex:0x29b566];
    [self.view addSubview:self.axLabel];
    
 /** 分段控制 */
    
    NSArray * btnDataSource = @[@"手机登录", @"账号登录"];
   self.segmentedView = [YJSegmentedControl segmentedControlFrame:CGRectMake(self.view.bounds.size.width/3.0, NAVIGATION_HEIGHT+160, self.view.bounds.size.width/3.0, 44) titleDataSource:btnDataSource backgroundColor:[UIColor colorWithRed:253.0f/255 green:239.0f/255 blue:230.0f/255 alpha:1.0f] titleColor:[UIColor grayColor] titleFont:[UIFont fontWithName:@".Helvetica Neue Interface" size:16.0f] selectColor:COLOR_GREEN buttonDownColor:COLOR_GREEN Delegate:self];
    // 第5步 添加到试图上
    [self.view addSubview:self.segmentedView];
    
    /** view */
    self.lgView = [[UIView alloc]init];
    self.lgView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.lgView];

    /** 手机号码Imageview */
    self.sjImageview = [[UIImageView alloc]init];
    self.sjImageview.image = [UIImage imageNamed:@"sjhm"];
    [self.lgView addSubview:self.sjImageview];
     /** 手机号码TextField */
    self.sjTextField = [[UITextField alloc]init];
    self.sjTextField.placeholder = @" 请输入手机号码";
    self.sjTextField.textColor = [UIColor colorWithHex:0x999999];
    self.sjTextField.cornerRadius = 10;
    self.sjTextField.backgroundColor = [UIColor colorWithHex:0xf8f8f8];
    [self.lgView addSubview:self.sjTextField];
    
    /** 验证码mageview */
    self.yzmImageview = [[UIImageView alloc]init];
    self.yzmImageview.image = [UIImage imageNamed:@"mm"];
    [self.lgView addSubview:self.yzmImageview];
     /** 验证码TextField */
    self.yzmTextField = [[UITextField alloc]init];
    self.yzmTextField.placeholder = @" 请输入验证码";
    self.yzmTextField.cornerRadius = 10;
    self.yzmTextField.backgroundColor = [UIColor colorWithHex:0xf8f8f8];
    self.yzmTextField.textColor = [UIColor colorWithHex:0x999999];
    [self.lgView addSubview:self.yzmTextField];
     /** 获取验证码 */
    self.hqyzmButton = [[UIButton alloc]init];
    [self.hqyzmButton addTarget:self action:@selector(hqyzm:) forControlEvents:UIControlEventTouchUpInside];
    [self.hqyzmButton setTitleColor:[UIColor colorWithHex:0x29b566] forState:UIControlStateNormal];
    [self.hqyzmButton setTitle:@"获取验证码" forState:UIControlStateNormal];
    self.hqyzmButton.titleLabel.font = FONT_SIXTEEN;
    [self.lgView addSubview:self.hqyzmButton];
    
    /** 登录 */
    self.loginButton = [[UIButton alloc]init];
    [self.loginButton addTarget:self action:@selector(dl:) forControlEvents:UIControlEventTouchUpInside];
    self.loginButton.backgroundColor = [UIColor colorWithHex:0x3bde72];
    [self.loginButton setTitle:@"登录" forState:UIControlStateNormal];
    [self.loginButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.loginButton.cornerRadius = 10;
    [self.lgView addSubview:self.loginButton];
    /** 注册 */
    self.registerButton = [[UIButton alloc]init];
    self.registerButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [self.registerButton addTarget:self action:@selector(zc:) forControlEvents:UIControlEventTouchUpInside];
    [self.registerButton setTitle:@"注册" forState:UIControlStateNormal];
    [self.registerButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    self.registerButton.titleLabel.font = [UIFont systemFontOfSize:20];
    [self.lgView addSubview:self.registerButton];
    /** 忘记密码 */
    self.forgetButton = [[UIButton alloc]init];
    [self.forgetButton addTarget:self action:@selector(wjmm:) forControlEvents:UIControlEventTouchUpInside];
    [self.forgetButton setTitle:@"忘记密码" forState:UIControlStateNormal];
    [self.forgetButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
    [self.forgetButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    self.forgetButton.titleLabel.font = [UIFont systemFontOfSize:20];
    [self.lgView addSubview:self.forgetButton];
    /** 第三方三个按钮 */
    self.qqButton = [[UIButton alloc]init];
    [self.qqButton addTarget:self action:@selector(qq:) forControlEvents:UIControlEventTouchUpInside];
    [self.qqButton setImage:[UIImage imageNamed:@"qq"] forState:UIControlStateNormal];
    [self.lgView addSubview:self.qqButton];
    /** 微信*/
    self.wxButton = [[UIButton alloc]init];
    [self.wxButton addTarget:self action:@selector(wx:) forControlEvents:UIControlEventTouchUpInside];
    [self.wxButton setImage:[UIImage imageNamed:@"wx"] forState:UIControlStateNormal];
    [self.lgView addSubview:self.wxButton];
    /**微博 */
    self.wbButton = [[UIButton alloc]init];
    [self.wbButton addTarget:self action:@selector(wb:) forControlEvents:UIControlEventTouchUpInside];
    [self.wbButton setImage:[UIImage imageNamed:@"wb"] forState:UIControlStateNormal];
    [self.lgView addSubview:self.wbButton];
    
    if (SCREEN_WIDTH <= 320) {
        
    }
    
}


- (void)setlayout {
    /** 登录logo */
    self.logoImageView.sd_layout
    .centerXEqualToView(self.view)
    .topSpaceToView(self.view, NAVIGATION_HEIGHT)
    .widthIs(100)
    .heightIs(100);
     /** 安心即刻启程 */
    self.axLabel.sd_layout
    .leftSpaceToView(self.view, 0)
    .topSpaceToView(self.logoImageView, 10)
    .widthIs(SCREEN_WIDTH)
    .heightIs(30);
 /** lgView */
    self.lgView.sd_layout
    .leftSpaceToView(self.view, 0)
    .topSpaceToView(self.segmentedView, 0)
    .rightSpaceToView(self.view, 0)
    .bottomSpaceToView(self.view, 0);
    /** 手机号码imageview*/
    self.sjImageview.sd_layout
    .leftSpaceToView(self.lgView, 30)
    .topSpaceToView(self.lgView, 20)
    .widthIs(30)
    .heightIs(30);
    /** 手机号码TextField*/
    self.sjTextField.sd_layout
    .leftSpaceToView(self.sjImageview, 10)
    .topSpaceToView(self.lgView, 15)
    .widthIs(SCREEN_WIDTH-140)
    .heightIs(40);
    /** 获取验证码 */
    self.hqyzmButton.sd_layout
    .rightSpaceToView(self.lgView, 50)
    .topSpaceToView(self.sjTextField, 0)
    .widthIs(100)
    .heightIs(30);
    /** 请输入验证码imageview*/
    self.yzmImageview.sd_layout
    .leftSpaceToView(self.lgView, 30)
    .topSpaceToView(self.hqyzmButton, 5)
    .widthRatioToView(self.sjImageview, 1)
    .heightRatioToView(self.sjImageview, 1);
    /** 请输入验证码TextField*/
    self.yzmTextField.sd_layout
    .leftSpaceToView(self.yzmImageview, 10)
    .topSpaceToView(self.hqyzmButton, 0)
    .widthRatioToView(self.sjTextField, 1)
    .heightRatioToView(self.sjTextField, 1);
    /** 登录*/
    self.loginButton.sd_layout
    .leftSpaceToView(self.lgView, 70)
    .topSpaceToView(self.yzmTextField, 40)
    .widthRatioToView(self.sjTextField, 1)
    .heightIs(40);
    /** 注册按钮 */
    self.registerButton.sd_layout
    .leftSpaceToView(self.lgView, 50)
    .topSpaceToView(self.loginButton, 20)
    .widthIs(100)
    .heightIs(30);
    /** 忘记密码*/
    self.forgetButton.sd_layout
    .rightSpaceToView(self.lgView, 50)
    .topEqualToView(self.registerButton)
    .widthIs(100)
    .heightIs(30);
    /** qq微信微博*/
    self.qqButton.sd_layout
    .leftSpaceToView(self.lgView, 50)
    .topSpaceToView(self.registerButton, 10)
    .widthIs(50)
    .heightIs(50);
    self.wxButton.sd_layout
    .centerXEqualToView(self.lgView)
    .topEqualToView(self.qqButton)
    .widthRatioToView(self.qqButton, 1)
    .heightIs(50);
    self.wbButton.sd_layout
    .rightSpaceToView(self.lgView, 50)
    .topEqualToView(self.qqButton)
    .widthRatioToView(self.qqButton, 1)
    .heightIs(50);
    
    if (SCREEN_WIDTH <= 320) {
        
        /** 登录*/
        self.loginButton.sd_layout
        .leftSpaceToView(self.lgView, 70)
        .topSpaceToView(self.yzmTextField, 40)
        .widthIs(SCREEN_WIDTH -140)
        .heightIs(30);
        /** 注册按钮 */
        self.registerButton.sd_layout
        .leftEqualToView(self.sjImageview)
        .topSpaceToView(self.loginButton, 20)
        .widthIs(100)
        .heightIs(20);
        /** 忘记密码*/
        self.forgetButton.sd_layout
        .rightSpaceToView(self.lgView, 50)
        .topEqualToView(self.registerButton)
        .widthIs(100)
        .heightIs(20);
        /** qq微信微博*/
        self.qqButton.sd_layout
        .leftSpaceToView(self.lgView, 50)
        .topSpaceToView(self.registerButton, 10)
        .widthIs(30)
        .heightIs(30);
        self.wxButton.sd_layout
        .centerXEqualToView(self.lgView)
        .topEqualToView(self.qqButton)
        .widthRatioToView(self.qqButton, 1)
        .heightIs(30);
        self.wbButton.sd_layout
        .rightSpaceToView(self.lgView, 50)
        .topEqualToView(self.qqButton)
        .widthRatioToView(self.qqButton, 1)
        .heightIs(30);
    }
}

#pragma mark -- 遵守代理 实现代理方法 分段控制的代理
- (void)segumentSelectionChange:(NSInteger)selection{
    
    if (selection == 0) {
        
        self.hqyzmButton.hidden = NO;
        self.sjTextField.placeholder = @" 请输入手机号码";
        self.yzmTextField.placeholder = @" 请输入验证码";
        
        NSLog(@"手机登录");
        
    }else if (selection == 1){
        
        self.hqyzmButton.hidden = YES;
        self.sjTextField.placeholder = @" 请输入账号";
        self.yzmTextField.placeholder = @" 请输入密码";
        
        NSLog(@"账号登录");
    }else{
        
        
    }
    
}


#pragma mark - Action
/** 登录时获取验证码 */
- (void)hqyzm:(UIButton *)btn {
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"sessionId"] = self.sessionId;
    params[@"fPhone"] = self.sjTextField.text;
    params[@"smsCodeType"] = @"LOGINCODE";
    [NetWorkHelp yl_postDataWith:params url:URL_SPLIT(URL_UserLogin, URL_UserLogin_notLoginSmsCode) success:^(id respose) {
        //判断验证码是否发送成功
        if ([respose[@"header"][@"statusCode"] integerValue] == 200) {
            
            [self.hqyzmButton setTitle:@"60s" forState:UIControlStateDisabled];
            self.hqyzmButton.backgroundColor = COLOR_ORG;
            [self.hqyzmButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            self.hqyzmButton.cornerRadius = 5;
            self.hqyzmButton.borderWidth = 0;
            self.hqyzmButton.enabled = NO;
            self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
            /** 获取网络请求得到sessionId */
            self.sessionId = respose[@"header"][@"sessionId"];
            
        }
        
    } failure:^(NSError *error) {
        
        NSLog(@"登录失败%@",error);
    }];
    
}


/** 倒计时的方法 */

- (void)timerAction:(NSString *)timer
{
    NSString *title = self.hqyzmButton.titleLabel.text;
    int s = [title intValue];
    s--;
    if (s > 0) {
        title = [NSString stringWithFormat:@"%is", s];
        [self.hqyzmButton setTitle:title forState:UIControlStateDisabled];
    } else {
        self.hqyzmButton.backgroundColor = White(255);
        self.hqyzmButton.borderWidth = 1;
        self.hqyzmButton.enabled = YES;
        [self.timer invalidate];
    }
}



/** 登录 */
- (void)dl:(UIButton *)btn {
    
    [[AppDelegate app] rootVCIsTabBar];
    
//    //获取输入的账号密码
//    NSString *fPhone = self.sjTextField.text;
//    NSString *fPassword = self.yzmTextField.text;
//    //请求的参数
//    NSMutableDictionary *params = [NSMutableDictionary dictionary];
//    params[@"fPhone"] = fPhone;
//    params[@"fPassword"] = fPassword;
//
//
//    if (fPhone.length <= 0) {
//
//
//
//        UIAlertController *alertController1 = [UIAlertController alertControllerWithTitle:@"提示" message:@"请输入账号！" preferredStyle:UIAlertControllerStyleAlert];
//        UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//            NSLog(@"点击了按钮1，进入按钮1的事件");
//        }];
//        UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
//            NSLog(@"点击了取消");
//        }];
//
//        [alertController1 addAction:action1];
//        [alertController1 addAction:action2];
//        [self presentViewController:alertController1 animated:YES completion:nil];
//
//        return;
//
//    }
//
//    else if (fPassword.length <= 0){
//
//        UIAlertController *alertController2 = [UIAlertController alertControllerWithTitle:@"提示" message:@"请输入密码！" preferredStyle:UIAlertControllerStyleAlert];
//        UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//            NSLog(@"点击了按钮1，进入按钮1的事件");
//        }];
//        UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
//            NSLog(@"点击了取消");
//        }];
//
//        [alertController2 addAction:action1];
//        [alertController2 addAction:action2];
//         [self presentViewController:alertController2 animated:YES completion:nil];
//
//
//        return;
//    }
//
//
//    [NetWorkHelp yl_postDataWith:params url:URL_SPLIT(URL_UserLogin, URL_UserLogin_namePaswLogin) success:^(id respose) {
//
//         if ([respose[@"header"][@"statusCode"] integerValue] == 200) {
//
//             /** 如果登录成功就进入主页面 */
//
//             [[AppDelegate app] rootVCIsTabBar];
//         }
//         else {
//
//
//             UIAlertController *alertController3 = [UIAlertController alertControllerWithTitle:@"提示" message:respose[@"header"][@"statusMessage"] preferredStyle:UIAlertControllerStyleAlert];
//             UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//                 NSLog(@"点击了按钮1，进入按钮1的事件");
//             }];
//             UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
//                 NSLog(@"点击了取消");
//             }];
//
//             [alertController3 addAction:action1];
//             [alertController3 addAction:action2];
//            [self presentViewController:alertController3 animated:YES completion:nil];
//
//         }
//
//
//        NSLog(@"登录成功%@",respose);
//
//    } failure:^(NSError *error) {
//
//        NSLog(@"登录失败%@",error);
//    }];
//
//

    
    
}

/** 注册 */
- (void)zc:(UIButton *)btn {
    
    AXRegisterViewController *resVC = [[AXRegisterViewController alloc]init];
    //resVC.hbd_barHidden = YES;
    resVC.hbd_barAlpha = 0;
    [self.navigationController pushViewController:resVC animated:YES];
    
}
/** 忘记密码 */
- (void)wjmm:(UIButton *)btn {
    
    AXFindPasswordViewController *findVC = [[AXFindPasswordViewController alloc]init];
    //findVC.hbd_barHidden = YES;
     findVC.hbd_barAlpha = 0;
    [self.navigationController pushViewController:findVC animated:YES];
}
/** qq */
- (void)qq:(UIButton *)btn {
    
}
/** 微信 */
- (void)wx:(UIButton *)btn {
    
}
/** 微博 */
- (void)wb:(UIButton *)btn {
    
}


#pragma mark - requestNetwork

- (void)requestLogin {
    
   
    
    
}

//点击空白处收起键盘
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    [self.view endEditing:YES];
}




@end
